package com.net.client.entity;

import com.net.common.exception.NetRuntimeException;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Getter
@Setter
public class ClientProperties {
    /**
     * 服务端ip
     */
    private String serverIp;
    /**
     * 服务端端口
     */
    private int serverPort;
    /**
     * 本地ip
     */
    private String localIp;
    /**
     * 本地端口
     */
    private int localPort;

    /**
     * 加载配置文件资源
     *
     * @param inputStream 资源流
     */
    @SneakyThrows
    public void loadResources(InputStream inputStream, ClientProperties clientProperties) {
        Properties properties = new Properties();
        if (Objects.isNull(inputStream)) {
            throw new NetRuntimeException("加载配置文件失败");
        }
        properties.load(inputStream);

        if (StringUtils.isBlank(properties.getProperty("server.ip")) || StringUtils.isBlank(properties.getProperty("server.port"))) {
            throw new NetRuntimeException("配置文件: server.ip 或 server.port 未配置");
        }
        if (StringUtils.isBlank(properties.getProperty("local.ip")) || StringUtils.isBlank(properties.getProperty("local.port"))) {
            throw new NetRuntimeException("配置文件: local.ip 或 local.port 未配置");
        }
        clientProperties.setServerIp(properties.getProperty("server.ip"));
        clientProperties.setServerPort(Integer.parseInt(properties.getProperty("server.port")));
        clientProperties.setLocalIp(properties.getProperty("local.ip"));
        clientProperties.setLocalPort(Integer.parseInt(properties.getProperty("local.port")));
    }
}
